<?php
require  Yii::getPathOfAlias('ext.QueryPath').'/QueryPath.php';
class DangdangCommand extends CConsoleCommand {
	private $url = "http://v.dangdang.com/";
	private $html;
	public function actionIndex(){
		$ch = curl_init();
		
		$options = array(
				CURLOPT_URL => $this->url,
// 				CURLOPT_POST => 1,
				CURLOPT_RETURNTRANSFER => true,
// 				CURLOPT_POSTFIELDS => $post  ,
// 				CURLOPT_HTTPHEADER =>$headerArr,
// 				CURLOPT_USERAGENT=>$userAgent
		);
		
		curl_setopt_array($ch, $options);
		$html = curl_exec($ch);
		
		curl_close($ch);
// 		$code=mb_detect_encoding($this->html,array('GBK','UTF-8','ASCII'));
// 		$html = mb_convert_encoding($html, 'UTF-8', 'GB2312,GBK,UTF-8,ASCII');
// 		$html = iconv('gbk', 'utf-8', $html);
		
		$html = file_get_contents($this->url);
		// 		 $html = mb_convert_encoding($html, 'UTF-8', 'GB2312,GBK,UTF-8,ASCII');
		// 		 $qp = qp($html, 'body');
		$htmlqp = htmlqp($html,"div.content-list");
		$lastest = $htmlqp->find('div.outlets_list');
		foreach ($lastest as $hi){
			$linkBranch = $hi->branch();
			$titleBranch = $hi->branch();
		
			$aLabel = $linkBranch->find('a');
		
			$brandLink = $aLabel->attr('href');
		
			$imgLabel =  $aLabel->find('img');
			//get image src
			$imgSrc = $imgLabel->attr('data-original');
			$imgSrc = empty($imgSrc)?$imgLabel->attr('src'):$imgSrc;
		
		
			$discount = $linkBranch->end()->find('span.re span')->text();
		
			$endtime = $titleBranch->find('span.time')->attr('endvalue');
			$campaignName = $titleBranch->end()->find('div.title')->find('span.name')->text();
		
		
			var_dump($brandLink,$imgSrc,$endtime,$discount,$campaignName);
			echo "<hr />";
		}
	}
	
	
	public function actionBrandList(){
		$url = "http://v.dangdang.com/7752_973_1";
		$html = file_get_contents($url);
		$detailProductList = htmlqp($html,'ul.list_aa li');
		foreach($detailProductList as $dp){
			$linkLabel = $dp->find('a');
			echo $productDetailUrl = $linkLabel->attr('href');
			$nameLabel = $dp->end()->find('.name a');
			echo $productName = $nameLabel->text();
			echo $productPrice = $nameLabel->attr('href');
			
			
			$priceLabel = $dp->end()->find('p.price');
			echo $priceLabel->find('span.rob .num')->text();
			echo $priceLabel->end()->find('span.price_r .num')->text();
			$discount =  $priceLabel->end()->find('span.price_s')->text();
// 			$d = substr($discount,0, -3);
			echo mb_substr($discount,0, -1,'utf-8');
// 			echo $priceLabel->text();
			echo "\n";
		}
		
	}
	
	
	public function actionDetail(){
		$url = "http://product.dangdang.com/1116270812.html";
		$html = file_get_contents($url);
		$html = mb_convert_encoding($html, 'UTF-8', 'GB2312,GBK,UTF-8,ASCII');
		$detailShow = htmlqp($html,'div.show');
		$showInfo = $detailShow->branch('.show_info');
		$picListLi = $detailShow->find('.pic_list li');
		
		$imgSrcList = array();
		foreach ($picListLi as $li){
			$wImgSrc = $li->find('a')->attr('id');
			$sImgSrc = $li->end()->find('img')->attr('src');
			$imgSrcList[] = array(
					'w'=>$wImgSrc,
					's'=>$sImgSrc
			);
		}
		echo $showInfo->find('.head h1')->text();
	}
}

?>